home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / ABUSESRC.ZIP / AbuseSrc / macabuse / imlib / unpackgl.c < prev    next >
C/C++ Source or Header  |  1997-05-20  |  2KB  |  88 lines

  1. #include "system.h"
  2. #include "macs.hpp"
  3. #include <stdio.h>
  4.  
  5. void unpackgl(char *fn, int pict_only)
  6. {
  7.   unsigned short dir_length,sl,x,amread,bufsize;
  8.   unsigned long offset,ret_ofs,length;
  9.   char name[14],st[50],*buf;
  10.   FILE *fp,*ot;
  11.   CHECK(fn);
  12.   fp=fopen(fn,"rb");
  13.   if (!fp) return ;
  14.  
  15.   if (fread(&dir_length,2,1,fp)!=1) return ;
  16.   dir_length=int_to_local(dir_length)/17-1;
  17.   ret_ofs=ftell(fp);
  18.   while (dir_length--)
  19.   {
  20.     fseek(fp,ret_ofs,SEEK_SET);
  21.     if (fread(&offset,4,1,fp)!=1) return ;
  22.     offset=long_to_local(offset);
  23.     if (fread(name,13,1,fp)!=1)  return ;
  24.     name[13]=0;
  25.     sl=strlen(name);
  26.     ret_ofs=ftell(fp);  // save the current spot in the file so we can come back to it later
  27.     if (toupper(name[sl-3])=='P' || toupper(name[sl-3])=='C' || !pict_only)
  28.     {
  29.       fseek(fp,offset,SEEK_SET);
  30.       fread(&length,1,4,fp);
  31.       length=long_to_local(length);
  32.       if (length>1000000)
  33.         exit(1);
  34.       ot=fopen(name,"rb");
  35.       if (ot)
  36.       { printf("File (%s) already exsist, overwrite (y/n)?\n",name);
  37.     fgets(st,49,stdin);
  38.     if (!(st[0]=='Y' || st[0]=='y'))
  39.       length=0;
  40.     fclose(ot);
  41.       }
  42.       if (length)
  43.     ot=fopen(name,"wb");
  44.       if (!ot) return ;
  45.       bufsize=0xf000;
  46.       do {
  47.     buf=(char *)jmalloc(bufsize,"unpack_gl::buffer");
  48.     if (!buf) bufsize-=100;
  49.       } while (!buf);
  50.  
  51.       while (length)
  52.       {
  53.     if (length>bufsize)
  54.       amread=bufsize;
  55.     else amread=length;
  56.     fread(buf,1,amread,fp);
  57.     fwrite(buf,1,amread,ot);
  58.     length-=amread;
  59.       }
  60.       jfree(buf);
  61.       if (ot) fclose(ot);
  62.     }
  63.   }
  64.   fclose(fp);
  65. }
  66.  
  67. main(int argc, char **argv)
  68. {
  69.   FILE *fp;
  70.   char fn[100];
  71.   int t,i,x;
  72.   if (argc<2)
  73.   { printf("Usage : unpackgl filename[.gl] [-p]\n");
  74.     printf("  -p only unpacks picture files\n");
  75.     printf("  Unpackes the files contain within a GL file (GRASP format)\n");
  76.     printf("  Does not change the orginal file\n");
  77.     exit(0);
  78.   }
  79.   strcpy(fn,argv[1]);
  80.   if (fn[strlen(fn)-3]!='.')
  81.     strcat(fn,".gl");
  82.   if (*argv[2]=='-' && *(argv[2]+1)=='p')
  83.     unpackgl(fn,1);
  84.   else unpackgl(fn,0);
  85.   return 0;
  86. }
  87.  
  88.